package com.kaili.basic.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.MdmDriveLoginLogDTO;
import com.kaili.basic.pojo.dto.MdmDriveLoginLogQueryDto;
import com.kaili.basic.pojo.dto.MdmSysLogQueryDto;
import com.kaili.basic.pojo.dto.MdmSysLoginLogQueryDto;
import com.kaili.basic.pojo.entity.MdmDriveLoginLog;
import com.kaili.basic.pojo.entity.MdmSysLoginLog;
import com.kaili.basic.pojo.vo.MdmSysLogExportVO;
import com.kaili.basic.service.IMdmLogService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.exception.RestException;
import com.kaili.common.pojo.entity.MdmSysLogEntity;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.CommonUtil;
import com.kaili.common.util.ExcelUtil;
import com.kaili.common.util.LoginDetailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@ApiVersion(1)
@RequestMapping("/{api_version}")
@RestController
@Api(tags = "日志管理", description = "日志管理相关API")
public class MdmLogController {
    @Autowired
    private IMdmLogService IMdmLogService;

    @GetMapping("/logs/sys")
    @ApiOperation(value = "分页查询-系统日志", notes = "分页查询-系统日志事件")
    public Page<MdmSysLogEntity> querySysLog(@ModelAttribute MdmSysLogQueryDto dto, @ApiParam(hidden = true)HttpServletRequest request) {
        return IMdmLogService.querySysLog(CommonUtil.getMongodbName(request,"sys_log"),dto);
    }

    @GetMapping("/logs/sys/export")
    @ApiOperation(value = "导出-系统日志", notes = "导出-系统日志事件")
    public void querySysLogExport(@ModelAttribute MdmSysLogQueryDto dto,
                                                  @ApiParam(hidden = true)HttpServletResponse response,
                                                  @ApiParam(hidden = true)HttpServletRequest request) throws RestException {
        List<MdmSysLogExportVO> list = IMdmLogService.querySysLogExport(CommonUtil.getMongodbName(request, "sys_log"), dto);
        ExcelUtil.exportExcel(list,"系统日志","sheet1", MdmSysLogExportVO.class, UUID.randomUUID().toString()+".xls",response);
    }

    @GetMapping("/logs/sysLogin")
    @ApiOperation(value = "分页查询-系统登录日志", notes = "分页查询-系统登录日志事件")
    public Page<MdmSysLoginLog> querySysLoginLog(@ModelAttribute MdmSysLoginLogQueryDto dto, @ApiParam(hidden = true)HttpServletRequest request) {
        String collectionName = CommonUtil.getMongodbName(request,"sys_login_log");
        return IMdmLogService.querySysLoginLog(dto,collectionName);
    }

    @GetMapping("/logs/sysLogin/export")
    @ApiOperation(value = "导出-系统登录日志", notes = "导出-系统登录日志事件")
    public void querySysLoginLogExport(@ModelAttribute MdmSysLoginLogQueryDto dto,
                                  @ApiParam(hidden = true)HttpServletResponse response,
                                  @ApiParam(hidden = true)HttpServletRequest request) throws RestException {
        List<MdmSysLoginLog> list = IMdmLogService.querySysLogExport(dto,CommonUtil.getMongodbName(request, "sys_login_log"));
        ExcelUtil.exportExcel(list,"系统登录日志","sheet1", MdmSysLoginLog.class, UUID.randomUUID().toString()+".xls",response);
    }

    @PostMapping("/logs/sysLogout")
    @ApiOperation(value = "保存系统-退出日志", notes = "保存系统-退出日志事件")
    public void queryLogOne(@ApiParam(hidden = true)HttpServletRequest request) {
        //保存退出日志
        String collectionName = CommonUtil.getMongodbName(request,"sys_login_log");
        MdmSysLoginLog logOne = IMdmLogService.queryLogOne(LoginDetailUtils.getCurrent().getUsername(), collectionName);
        logOne.setLogoutTime(new Date());
        IMdmLogService.saveSysLoginLog(logOne,collectionName);
    }

    @PostMapping("/logs/drive")
    @ApiOperation(value = "新增设备注册日志", notes = "新增设备注册日志事件")
    public RestStatus saveDriveLogs(@RequestBody MdmDriveLoginLogDTO dto, @ApiParam(hidden = true)HttpServletRequest request) {
         IMdmLogService.saveDriveLogs(dto,CommonUtil.getMongodbName(request,"drive_login_log"));
         return CommonStatus.ADD_OK;
    }

    @GetMapping("/logs/drive")
    @ApiOperation(value = "分页查询-设备日志", notes = "分页查询-设备日志事件")
    public Page<MdmDriveLoginLog> queryDriveLogs(@ModelAttribute MdmDriveLoginLogQueryDto queryDto,
                                                 @ApiParam(hidden = true)HttpServletRequest request) {
        return IMdmLogService.queryDriveLogs(CommonUtil.getMongodbName(request,"drive_login_log"),queryDto);
    }

    @GetMapping("/logs/drive/export")
    @ApiOperation(value = "导出-设备日志", notes = "分页查询-设备日志事件")
    public void queryDriveLogExport(@ModelAttribute MdmDriveLoginLogQueryDto dto,
                                       @ApiParam(hidden = true)HttpServletResponse response,
                                       @ApiParam(hidden = true)HttpServletRequest request) throws RestException {
        List<MdmDriveLoginLog> list = IMdmLogService.queryDriveLogExport(dto,CommonUtil.getMongodbName(request, "drive_login_log"));
        ExcelUtil.exportExcel(list,"设备登录日志","sheet1", MdmDriveLoginLog.class, UUID.randomUUID().toString()+".xls",response);
    }
}
